package com.hkzhao.reportingsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.hkzhao.reportingsystem.common.api.CommonResult;
import com.hkzhao.reportingsystem.common.utils.TranslateToToken;
import com.hkzhao.reportingsystem.mbg.mapper.CommentMapper;
import com.hkzhao.reportingsystem.mbg.mapper.NewsMapper;
import com.hkzhao.reportingsystem.mbg.mapper.UserMapper;
import com.hkzhao.reportingsystem.mbg.model.*;
import com.hkzhao.reportingsystem.service.NormalUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NormalUserServiceImpl implements NormalUserService {
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private NewsMapper newsMapper;
    @Autowired(required = false)
    private CommentMapper commentMapper;
    @Override
    public boolean userlogin(String openid, String username) {
        if(getUserFromToken(openid)!=null)
            return true;
        User user = new User();
        user.setToken(openid);
        user.setUsername(username);
        userMapper.insert(user);
        return true;
    }

    @Override
    public User getUserFromToken(String openid) {
        User user = userMapper.selectByPrimaryKey(openid);
        return user;
    }

    @Override
    public List<News> getListNews(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return newsMapper.selectByExample(new NewsExample());
    }

    @Override
    public List<Comment> getCommentListByNews(int pageNum, int pageSize, String news_id) {
        PageHelper.startPage(pageNum,pageSize);
        CommentExample c = new CommentExample();
        c.createCriteria().andNewsIdEqualTo(news_id);
        return commentMapper.selectByExample(c);
    }

    @Override
    public List<Comment> getCommentListByUser(int pageNum, int pageSize, String user_token) {
         PageHelper.startPage(pageNum,pageSize);
         CommentExample c = new CommentExample();
         c.createCriteria().andUserTokenEqualTo(user_token);
         return commentMapper.selectByExample(c);
    }

    @Override
    public CommonResult deleteCommentByUser(String comment_id, String user_token) {
        Comment c = commentMapper.selectByPrimaryKey(comment_id);
        if(c.getUserToken().equals(user_token))
        {
            commentMapper.deleteByPrimaryKey(comment_id);
            return CommonResult.success(c.getCommentId(),"成功删除此条评论");
        }
        return CommonResult.failed("user_token没有此条评论");
    }

    @Override
    public CommonResult addCommentByUserAndNew(String user_token, String news_id,String text) {
        Comment comment = new Comment();
        String s = TranslateToToken.GetGUID();
        comment.setCommentId(s);comment.setStars(0);comment.setText(text);comment.setTimestamp(new Date());
        comment.setUserToken(user_token);comment.setNewsId(news_id);
        commentMapper.insert(comment);
        return CommonResult.success(s,"成功插入此条评论");
    }
}
